package com.maiji.cloud.controller;

import com.maiji.cloud.entities.login.*;
import com.maiji.cloud.request.BaseDataReqDto;
import com.maiji.cloud.response.BaseDataResDto;
import com.maiji.cloud.response.BaseResDto;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.response.login.ActivityShareRuleConfigResData;
import com.maiji.cloud.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/maijiSettings/")
public class MaijiSettingsController {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private WeightChangeLogService weightChangeLogService;
    @Autowired
    private ShareWeightService shareWeightService;
    @Autowired
    private ActivityShareRuleConfigService activityShareRuleConfigService;
    @Autowired
    private UserService userService;
    @Autowired
    private SignInDeployService signInDeployService;

    /**
     * 获取基础减肥基金规则修改记录列表
     * @return
     */
    @PostMapping("findAllShareWeightRuleLogs")
    public BaseDataResDto<List<ShareWeightRuleEntity>> findAllShareWeightRuleLogs(@RequestBody BaseDataReqDto<List<Date>> baseDataReqDto){
        List<Date> data = baseDataReqDto.getData();
        return weightChangeLogService.findAllShareWeightRuleLogs(data);
    }

    /**
     * 修改基础减肥基金规则
     * @return
     */
    @PostMapping("updShareWeightRule")
    public BaseResDto updShareWeightRule(@RequestBody BaseDataReqDto<ShareWeightRuleEntity> baseDataReqDto){
        ShareWeightRuleEntity data = baseDataReqDto.getData();
        return weightChangeLogService.updShareWeightRule(data);
    }

    /**
     * 获取基础减肥基金规则
     * @return
     */
    @PostMapping("findShareWeightRule")
    public BaseDataResDto<ShareWeightRuleEntity> findShareWeightRule(){
        return weightChangeLogService.findShareWeightRule();
    }

    /**
     * 获取连续签到得积分规则修改记录列表
     * @return
     */
    @PostMapping("findAllSignInDeployLogs")
    public BaseDataResDto<List<SignInDeployEntity>> findAllSignInDeployLogs(@RequestBody BaseDataReqDto<List<Date>> baseDataReqDto){
        List<Date> data = baseDataReqDto.getData();
        return signInDeployService.findAllSignInDeployLogs(data);
    }

    /**
     * 修改连续签到得积分规则
     * @return
     */
    @PostMapping("editSignInDeploy")
    public BaseResDto editSignInDeploy(@RequestBody BaseDataReqDto<SignInDeployEntity> baseDataReqDto){
        SignInDeployEntity data = baseDataReqDto.getData();
        return signInDeployService.editSignInDeploy(data);
    }

    /**
     * 获取连续签到得积分规则
     * @return
     */
    @PostMapping("findAllSignInDeploys")
    public BaseDataResDto<List<SignInDeployEntity>> findAllSignInDeploys(){
        return signInDeployService.findAllSignInDeploys();
    }

    /**
     * 获取减重分享规则修改记录列表
     * @return
     */
    @PostMapping("findAllShareWeightHelpRuleLogs")
    public BaseDataResDto<List<ShareWeightHelpRule>> findAllShareWeightHelpRuleLogs(@RequestBody BaseDataReqDto<List<Date>> baseDataReqDto){
        List<Date> data = baseDataReqDto.getData();
        return shareWeightService.findAllShareWeightHelpRuleLogs(data);
    }

    /**
     * 修改减重分享规则
     * @return
     */
    @PostMapping("updShareWeightHelpRule")
    public BaseResDto updShareWeightHelpRule (@RequestBody BaseDataReqDto<ShareWeightHelpRule> baseDataReqDto){
        ShareWeightHelpRule data = baseDataReqDto.getData();
        return shareWeightService.updShareWeightHelpRule(data);
    }

    /**
     * 获取减重分享规则
     * @return
     */
    @PostMapping("findShareWeightHelpRule")
    public BaseDataResDto<ShareWeightHelpRule> findShareWeightHelpRule () {
        return shareWeightService.findShareWeightHelpRule();
    }

    /**
     * 获取分享封面和页面配置
     * @return
     */
    @PostMapping("findShareRuleConfig")
    public BaseDataResDto<ActivityShareRuleConfigResData> findShareRuleConfig (@RequestBody BaseDataReqDto<Integer> baseDataReqDto) {
        Integer type = baseDataReqDto.getData();
        return activityShareRuleConfigService.getActivityShareRuleConfigByType(type);
    }

    /**
     * 修改分享封面和页面配置
     * @return
     */
    @PostMapping("add_updShareRuleConfig")
    public BaseResDto add_updShareRuleConfig (@RequestBody BaseDataReqDto<ActivityShareRuleConfigEntity> baseDataReqDto){
        try {
            ActivityShareRuleConfigEntity data = baseDataReqDto.getData();
            return activityShareRuleConfigService.add_updActivityShareRuleConfig(data);
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseResDto(Status.ERROR);
        }
    }

    /**
     *    分销配置
     * @return
     */
    @PostMapping("setDistributionRuleConfig")
    public BaseResDto setDistributionRuleConfig(@RequestBody BaseDataReqDto<DistributionRuleConfigEntity> param){
    	if(param.getData() == null  || param.getData().getNum() == null || param.getData().getUnit() == null) return new BaseResDto(Status.PARAMETERERROR);
		return userService.setDistributionRuleConfig(param);
    }
    
    /**
     *    获取分销配置
     * @return
     */
    @PostMapping("getDistributionRuleConfig")
    public BaseDataResDto<DistributionRuleConfigEntity> getDistributionRuleConfig(){
    	DistributionRuleConfigEntity distributionRuleConfigEntity = mongoTemplate.findOne(new Query(), DistributionRuleConfigEntity.class, DistributionRuleConfigEntity.TABLE_NAME);
    	return new BaseDataResDto<DistributionRuleConfigEntity>(Status.SUCCESS).setData(distributionRuleConfigEntity);
    }
    
}

